package demo.dao;

import demo.dao.dto.Alert;
import demo.dao.mapper.AlertMapper;
import demo.vo.AlertStatus;
import demo.vo.common.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AlertDao {

    private static final Logger logger = LoggerFactory.getLogger(AlertDao.class);

    private AlertMapper alertMapper;


    @Autowired
    public void setAlertMapper(AlertMapper alertMapper) {
        this.alertMapper = alertMapper;
    }

    /**
     * 新增报警记录
     *
     * @param alert
     * @return
     */
    public int addAlert(Alert alert) {
        if (alert == null) {
            return 0;
        }
        return alertMapper.insertSelective(alert);
    }

    /**
     * 更新报警记录
     *
     * @param id
     * @param vo
     * @return
     */
    public int updateAlertById(int id, Alert vo) {
        if (vo == null) {
            return 0;
        }
        vo.setId(id);
        return alertMapper.updateByPrimaryKeySelective(vo);
    }

    /**
     * 通过结束时间查询
     *
     * @param start
     * @param end
     * @return
     */
    public List<Alert> queryByEndtime(long start, long end, AlertStatus status) {
        Map<String, Object> map = buildQueryParam(null, null, start, end);
        if (status != null) {
            map.put("state", status.name());
        }
        return alertMapper.selectByMap(map);
    }

    /**
     * 分页查询
     *
     * @param start     开始时间
     * @param end       结束时间
     * @param status    报警状态
     * @param groupName 报警组
     * @param app       系统
     * @param pageNum   页码
     * @param pageSize  每页数量
     * @return
     */
    public Page<Alert> pageBy(long start, long end, AlertStatus status,
                              String groupName, String app,
                              int pageNum, int pageSize) {
        Map<String, Object> map = buildQueryParam(null, null, start, end);
        appendQueryParam(map, app, null, null, null, groupName);
        if (status != null) {
            map.put("state", status.name());
        }
        int cnt = countBy(map);
        if (cnt == 0) {
            return new Page<>(pageNum, pageSize, cnt, Collections.emptyList());
        }
        List<Alert> list = queryBy(map, pageNum, pageSize);
        return new Page<>(pageNum, pageSize, cnt, list);
    }

    public int countBy(Map<String, Object> map) {
        if (map == null) {
            return 0;
        }
        return alertMapper.countByMap(map);
    }

    /**
     * 查询
     *
     * @param map
     * @param pageNum
     * @param pageSize
     * @return
     */
    public List<Alert> queryBy(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        if (map == null) {
            return Collections.emptyList();
        }
        if (pageNum != null && pageSize != null) {
            map.put("offset", Page.offset(pageNum, pageSize));
            map.put("rows", pageSize);
        }
        return alertMapper.selectByMap(map);
    }


    /**
     * 构建查询参数
     *
     * @param app     系统
     * @param service 服务
     * @param target  目标
     * @param tag     报警类型标签
     * @return
     */
    private static void appendQueryParam(Map<String, Object> map, String app, String service, String target, String tag, String alertGroup) {
        if (StringUtils.isNotBlank(app)) {
            map.put("app", app);
        }
        if (StringUtils.isNotBlank(service)) {
            map.put("service", service);
        }
        if (StringUtils.isNotBlank(target)) {
            map.put("target", target);
        }
        if (StringUtils.isNotBlank(tag)) {
            map.put("tag", tag);
        }
        if (StringUtils.isNotBlank(alertGroup)) {
            map.put("alertGroup", alertGroup);
        }
    }

    private Map<String, Object> buildQueryParam(Long minStartTime, Long maxStartTime, Long minEndTime, Long maxEndTime) {
        Map<String, Object> map = new HashMap<>();
        if (minStartTime != null) {
            map.put("minStartTime", new Date(minStartTime));
        }
        if (maxStartTime != null) {
            map.put("maxStartTime", new Date(maxStartTime));
        }
        if (minEndTime != null) {
            map.put("minEndTime", new Date(minEndTime));
        }
        if (maxEndTime != null) {
            map.put("maxEndTime", new Date(maxEndTime));
        }
        return map;
    }


}
